

from Cheetah.Template import Template
import table_meta as meta


def test():
    uri = 'sqlite:///data.dm1'
    #uri = 'mysql://root:myjsy03bjwqt@127.0.0.1:3336/mymodel?charset=utf8&use_unicode=0'

    db = meta.DB(uri, False)
    db.exists()    
    db.source('metadata.sql')
    db.insert()

def getMeta():
    uri = 'sqlite:///data.dm1'
    #uri = 'mysql://root:myjsy03bjwqt@127.0.0.1:3336/mymodel?charset=utf8&use_unicode=0'
    db = meta.DB(uri, False)
    db.select()

def getmsg():
  sql ="select tablename, name, attribute  from columns where column_type != 'auto' and column_type != '' order by tablename, ordering asc "
  uri = 'sqlite:///data.dm1'
  db = meta.DB(uri, False)
  result = db.execute(sql)
  messages = []
  for row in result.fetchall():
    #print row[1].encode('utf8')
    messages.append((row[0], row[1],row[2]))
  #print messages
  return messages

def settest():
  pass

def getmsgdict():
  sql ="select tablename, name, attribute  from columns where column_type != 'auto' and column_type != '' order by tablename, ordering asc "
  uri = 'sqlite:///data.dm1'
  db = meta.DB(uri, False)
  result = db.execute(sql)
  messages = {}
  #ukey = set()
  i = 0
  for row in result.fetchall():
    #print row[1].encode('utf8')
    i = i+1
    key = row[1].lower()
    value = repr(row[2]).replace("u'","")
    value = value.replace("'","")
    messages[key] = value
  
  # username = \u7528\u6237\u540D
  print messages
  #print i
  #print len(messages)
  #print ukey
  return messages


def vmgen(messages):
  pathin  =  'mod_template/java/vm/edit.vm'
  pathout = 'editg.vm'
  output = open(pathout, 'w') 
  sp = Template(file=pathin)
  sp.messages = messages
  sp = "%s" %(sp)
  sp = sp.encode('utf8')
  print sp
  output.writelines(sp)

def pgen(messages):
  pathin  =  'mod_template/java/vm/editp.vm'
  pathout = 'editp.vm'
  output = open(pathout, 'w') 
  sp = Template(file=pathin)
  sp.messages = messages
  sp = "%s" %(sp)
  sp = sp.encode('utf8')
  print sp
  output.writelines(sp)

def formgen(messages):
  pathin  =  'form.tpl'
  pathout = 'form.jsp'
  output = open(pathout, 'w') 
  sp = Template(file=pathin)
  sp.messages = messages
  sp = "%s" %(sp)
  #print sp
  #output.writelines(sp)

def output(messages):
  pathin  =  'message_zh_CN.properties.tpl'
  pathout = 'message_zh_CN.properties'
  output = open(pathout, 'w') 
  sp = ""
  for item in messages:
    str = "msg.%s.%s = %s\n" %(item[0], item[1], item[2])
    output.write(str.encode('utf8'))
    
  #output.writelines(sp)

if __name__ == "__main__":
    #test()
    #getMeta()
    
    #output(getmsg())
    #formgen(getmsgdict())
    #vmgen(getmsgdict())
    #pgen(getmsgdict())
    #getmsgdict()
    settest()
